package com.tanhua.server.controller;

import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private MovementsService movementsService;

    /**
     * 查询单条动态评论列表
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping()
    public ResponseEntity<PageResult> queryCommentsList(@RequestParam("movementId") String publishId,
                                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult=this.commentsService.queryCommentsList(publishId,page,pageSize);
            if (null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 发布评论
     * @param params
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveComments(@RequestBody Map<String,String> params){
        try {
            String movementId = params.get("movementId");
            String content = params.get("comment");
            boolean b=this.commentsService.saveComments(movementId,content);
            if (b){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 给评论点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.likeComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消评论点赞
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> dislikeComment(@PathVariable("id") String publishId){
        Long count=this.movementsService.dislikeComment(publishId);
        if (null!=count){
            return ResponseEntity.ok(count);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
